﻿package src.hardware.rgbledmatrixdisplay 
{
	import flash.display.Sprite;
	
	/**
	* this is an individual led 
	* @author Louis Tovar
	*/
	public class LED extends Sprite
	{
		public static const EMULATOR_LOWEST_COLOR_PER_CHANNEL:uint = 0x30;
		
		private var _radius:Number
		
		private var led:Sprite;
		private var ledColor:uint;
		
		public function LED(p_radius:Number) 
		{
			_radius = p_radius;
			initSelf();
		}
		
		private function initSelf():void
		{
			
			ledColor = 0x303030;
			
			led = new Sprite();
			led.graphics.lineStyle(1, 0x000000);
			led.graphics.beginFill(ledColor, 1);
			led.graphics.drawCircle( radius, radius, radius);
			led.graphics.endFill();
			
			addChild(led);
		}
		
		public function setLEDColor(p_color:uint):void
		{
			if (Main.EMULATOR_COLOR_RESTRICTION)
			{
				var r:uint = p_color >> 16;
				var g:uint = p_color >> 8 & 0xFF;
				var b:uint = p_color & 0xFF;
				var colorCycler:Array = [r,g,b]
				
				for (var i:int = 0; i < colorCycler.length; i++) 
				{
					if (colorCycler[i] < EMULATOR_LOWEST_COLOR_PER_CHANNEL)
					{
						colorCycler[i] = EMULATOR_LOWEST_COLOR_PER_CHANNEL;
					}
				}
				
				r = colorCycler[0];
				g = colorCycler[1];
				b = colorCycler[2];
				
				p_color = r << 16 | g << 8 | b;

			}
			
			ledColor = p_color;
			
			led.graphics.clear();
			led.graphics.lineStyle(1, 0x000000);
			led.graphics.beginFill(ledColor, 1);
			led.graphics.drawCircle( radius, radius, radius);
			led.graphics.endFill();
		}
		
		public function getLEDColor():uint
		{
			return ledColor;
		}
		
		public function get radius():Number { return _radius; }
	}
	
}